package com.lvtongka.app.common

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import com.lvtongka.app.R
import com.lvtongka.app.base.BaseApplication
import java.net.URISyntaxException


class NaviUtils {
    companion object {
        private const val BAIDU_MAP_NAVI_URI = "baidumap://map/navi?query="
        //map app包名
        private const val BAIDU_MAP_APP = "com.baidu.BaiduMap"
        private const val GAODE_MAP_APP = "com.autonavi.minimap"
        private const val TENCENT_MAP_APP = "com.tencent.map"

        // 选中百度地图
        fun selectBD(context: Context, lat: String?, lon: String?, startName: String, endName: String?) {
            try {
                //调起App
                if (AppUtils.checkAppInstalled(BAIDU_MAP_APP)) {
                        val intent = Intent()
                        intent.setData(Uri.parse(BAIDU_MAP_NAVI_URI + endName))
                    context.startActivity(intent)
                } else {
                    Toast.makeText(context, "您还没有安装百度地图APP", Toast.LENGTH_SHORT).show()
                }
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
                Toast.makeText(context, "启动地图失败", Toast.LENGTH_SHORT).show()
            }
        }

        // 选中高德地图
        fun selectGD(context: Context, lat: String?, lon: String?, startName: String, endName: String?) {
            if (AppUtils.checkAppInstalled(GAODE_MAP_APP)) {
                try {
                    val intentOther = Intent(
                        "android.intent.action.VIEW",
                        Uri.parse(
                            (("amapuri://route/plan/?sid=BGVIS1&sname=" + startName + "&did=BGVIS2&dlat=" + lat)+
                                    "&dlon=" + lon) + "&dname=" + endName + "&dev=0&t=1"
                        )
                    ) //直接跳转至导航
                    intentOther.addCategory("android.intent.category.DEFAULT")
                    intentOther.setPackage("com.autonavi.minimap")
                    intentOther.setAction("android.intent.action.VIEW")
                    intentOther.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                    context.startActivity(intentOther)
                } catch (e: Exception) {
                    Toast.makeText(context, "启动地图失败", Toast.LENGTH_SHORT).show()
                }
            } else {
                Toast.makeText(context, "您还没有安装高德地图APP",Toast.LENGTH_SHORT).show()
            }
        }

        // 选中腾讯地图
        fun selectTencent(context: Context, lat: String?, lon: String?, startName: String, endName: String?) {
            if (AppUtils.checkAppInstalled(TENCENT_MAP_APP)) {
                try {
                    val URL = (((("qqmap://map/routeplan?type=drive&" +
                            "from=" + startName + "&fromcoord=" + startName + "," + endName) + "&to=" + endName + "&" +
                            "tocoord=" + lat) + "," + lon) + "&policy=1&referer=" + context.getResources()
                        .getString(R.string.app_name))
                    val intent = Intent.parseUri(URL, 0)
                    context.startActivity(intent)
                } catch (e: URISyntaxException) {
                    // TODO Auto-generated catch block
                    e.printStackTrace()
                    Toast.makeText(context, "启动地图失败", Toast.LENGTH_SHORT).show()
                }
            } else {
                Toast.makeText(context, "您还没有安装腾讯地图APP", Toast.LENGTH_SHORT).show()
            }
        }


    }
}